package com.basic.util;

import java.io.File;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.os.StatFs;

public class SdTool {
	
	/**
	 * @return Sd卡路径
	 */
	public static File getSDCardFile() {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			return Environment.getExternalStorageDirectory();
		} else {
			return null;
		}
	}
	
	/**
	 * @return SD卡空余控件 单位k
	 */
	public static float getSDCardFreeSizeKB() {
		StatFs sdcardFs = doGetSDcardFs();
		if (sdcardFs == null)
			return 0;
		return ((float) sdcardFs.getAvailableBlocks() / 1024)
				* ((float) sdcardFs.getBlockSize());
	}

	public static long getSDCardFreeSize() {
		StatFs sdcardFs = doGetSDcardFs();
		if (sdcardFs == null)
			return 0;
		return (long) sdcardFs.getAvailableBlocks() * sdcardFs.getBlockSize();
	}
	
	/**
	 * @return SD总容量 b
	 */
	public static long getSDCardTotalSize() {
		StatFs sdcardFs = doGetSDcardFs();
		if (sdcardFs == null)
			return 0;
		return (long) sdcardFs.getBlockCount() * sdcardFs.getBlockSize();
	}

	private static StatFs doGetSDcardFs() {
		File sdcardFile = getSDCardFile();
		if (sdcardFile == null)
			return null;
		return new StatFs(sdcardFile.getAbsolutePath());
	}

	public static float getSystemFreeSizeKB() {
		StatFs dataFs = doGetSystemFs();
		return ((float) dataFs.getAvailableBlocks() / 1024)
				* ((float) dataFs.getBlockSize());
	}

	public static long getSystemFreeSize() {
		StatFs dataFs = doGetSystemFs();
		return (long) dataFs.getAvailableBlocks() * dataFs.getBlockSize();
	}

	public static long getSystemTotalSize() {
		StatFs dataFs = doGetSystemFs();
		return (long) dataFs.getBlockCount() * dataFs.getBlockSize();
	}

	private static StatFs doGetSystemFs() {
		return new StatFs(Environment.getDataDirectory().getAbsolutePath());
	}
}
